package br.unicarioca.viaexpositus

class RelatorioController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    static defaultAction = "list"

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [relatorioInstanceList: Relatorio.list(params), relatorioInstanceTotal: Relatorio.count()]
    }

    def save = {
        def relatorioInstance = new Relatorio(params)
        if (relatorioInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'relatorio.label', default: 'Relatorio'), relatorioInstance.id])}"
            redirect(action: "list")
        } else {
            render(view: "edit", model: [relatorioInstance: relatorioInstance])
        }
    }


    def edit = {

        if(params.id && params.id != "0"){
            def relatorioInstance = Relatorio.get(params.id)
            if (relatorioInstance) {
                return [ relatorioInstance : relatorioInstance]
            }else{
                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'relatorio.label', default: 'Relatorio'), params.id])}"
                redirect(action: "list")
            }
        } else {
            def relatorioInstance = new Relatorio()
            return [relatorioInstance: relatorioInstance]
        }
    }

    def update = {
        def relatorioInstance = Relatorio.get(params.id)
        if (relatorioInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (relatorioInstance.version > version) {
                    
                    relatorioInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'relatorio.label', default: 'Relatorio')] as Object[], "Another user has updated this Relatorio while you were editing")
                    render(view: "edit", model: [relatorioInstance: relatorioInstance])
                    return
                }
            }
            relatorioInstance.properties = params
            if (!relatorioInstance.hasErrors() && relatorioInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'relatorio.label', default: 'Relatorio'), relatorioInstance.id])}"
                redirect(action: "edit", id: relatorioInstance.id)
            }
            else {
                render(view: "edit", model: [relatorioInstance: relatorioInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'relatorio.label', default: 'Relatorio'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def relatorioInstance = Relatorio.get(params.id)
        if (relatorioInstance) {
            try {
                relatorioInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'relatorio.label', default: 'Relatorio'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'relatorio.label', default: 'Relatorio'), params.id])}"
                redirect(action: "edit", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'relatorio.label', default: 'Relatorio'), params.id])}"
            redirect(action: "list")
        }
    }

    def showRelatorioAses = {

        if(params.id && params.id != "0"){
            def relatorioAsesInstance = RelatorioAses.get(params.id)
            if (relatorioAsesInstance) {
                render("view":"relatorioAses", "model":[ relatorio : relatorioAsesInstance])
            }else{
                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'relatorioAses.label', default: 'RelatorioAses'), params.id])}"
                redirect(action: "list")
            }

        } else{
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'relatorioAses.label', default: 'RelatorioAses'), params.id])}"
            redirect(action: "list")
        }
    }

}
